﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Net;
using System.IO;

namespace PRScreenWithKinect
{
    class QueryDB
    {
        string base_url  = GlobalConfig.SERVER_IP;
        int CLIENT_ID = GlobalConfig.CLIENT_ID;
        private String getResponse(string url){
            try
            {
                StringBuilder sb = new StringBuilder();
                byte[] buf = new byte[8192]; // used on each read operation
                HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();
                Stream resStream = response.GetResponseStream();//get response into stream
                string tempString = null;
                int count = 0;

                do
                {
                    count = resStream.Read(buf, 0, buf.Length); //read data via the response stream <data will store into buf>
                    if (count != 0)//make sure we read some data
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);
                        sb.Append(tempString);
                    }
                }
                while (count > 0); //if buffer size isn't sufficient for all data, then loop back to read remain data.
                return sb.ToString(); 
            }
            catch
            {

            }
            return "";
        }

        public NewsModel[] getNewsModel()
        {
            String url = base_url+"index.php/query/news";
            String result = getResponse(url);
            NewsModel[] allNews = JsonConvert.DeserializeObject<NewsModel[]>(result);
            return allNews;
        }

        public BillboardModel[] getBillboardModel()
        {
            String url = base_url+"/index.php/query/billboard";
            String result = getResponse(url);
            BillboardModel[] allBillBoard = JsonConvert.DeserializeObject<BillboardModel[]>(result);
            return allBillBoard;
        }

        public LocationModel[] getLocationModel()
        {
            String url =  base_url+"index.php/query/location";
            String result = getResponse(url);
            LocationModel[] allLocation = JsonConvert.DeserializeObject<LocationModel[]>(result);
            return allLocation;
        }

        public MarqueeTextModel[] getMarqueeTextModel()
        {
            String url =    base_url+"index.php/query/marqueeText" ;
            String result = getResponse(url);
            MarqueeTextModel[] allMarqueeText = JsonConvert.DeserializeObject<MarqueeTextModel[]>(result);
            return allMarqueeText;
        }

        public GuestBookModel[] getGuestBookModel()
        {
            String url = base_url + "index.php/query/guestbook";
            String result = getResponse(url);
            GuestBookModel[] allGuestBook = JsonConvert.DeserializeObject<GuestBookModel[]>(result);
            return allGuestBook;
        }

        public GalleryModel[] getGalleryModel()
        {
            String url = base_url + "index.php/query/gallery";
            String result = getResponse(url);
            GalleryModel[] allGallery = JsonConvert.DeserializeObject<GalleryModel[]>(result);
            return allGallery;
        }

        public GuestBookModel getLastGuestBook()
        {
            String url = base_url + "index.php/query/guestbook_last";
            String result = getResponse(url);
            GuestBookModel GuestBook = JsonConvert.DeserializeObject<GuestBookModel>(result);
            return GuestBook;
        }

        public AboutFacModel[] getAboutFacModel()
        {
            String url = base_url + "index.php/query/aboutfac";
            String result = getResponse(url);
            AboutFacModel[] allAboutFac = JsonConvert.DeserializeObject<AboutFacModel[]>(result);
            return allAboutFac;
        }

        public string getQRKey()
        {
            string url = base_url + "index.php/query/qr_key?client_id="+CLIENT_ID;
            string result = getResponse(url);
            string QRKey = JsonConvert.DeserializeObject<string>(result);
            return QRKey;
        }

        public string updateAndGetQRKey()
        {
            string url = base_url + "index.php/query/qr_update?client_id=" + CLIENT_ID;
            string result = getResponse(url);
            string QRKey = JsonConvert.DeserializeObject<string>(result);
            return QRKey;
        }


    }
}
